/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package HumanCentredSystemDesign;

import java.util.*;

/**
 *
 * @author Domonic
 */
public class IssueManager extends LibraryCirculations {

    //MB and chris wrote this 1/12/2011
    
    Item myItem;
    Date d;
    
    public void issueCopy(String _borrowerID, String _copyDewey){
        try{
            try{
            myBorrower = getBorrower(_borrowerID, "", "", "").get(0);
            }
            catch(Exception err){
                String errString = "Borrower Does Not Exist: " + err.toString();
                AlertWindow aw = new AlertWindow(errString);
                aw.setVisible(true);
                System.out.println(errString);
                return;
            }
            // Get some Item properties based on DEWEY of the copy
            try{
            myCopy = getCopyFromDewey(_copyDewey); // Get copy details from copyDewey        
            }
            catch(Exception err){
                String errString = "Dewey does not exist in the database: " + err.toString();
                AlertWindow aw = new AlertWindow(errString);
                aw.setVisible(true);
                System.out.println(errString);
                return;
            }
            try{
            myItem = getItemFromCopy(myCopy);
            }
            catch(Exception err){
                String errString = "Error: " + err.toString();
                AlertWindow aw = new AlertWindow(errString);
                aw.setVisible(true);
                System.out.println(errString);
            }

            Date myDate = new Date();
            int loanPeriod;     
            loanPeriod = 21;
            
            myLoan = new Loan(myDate,_copyDewey, _borrowerID, loanPeriod);
            
            if (canBorrow(myItem, myBorrower) && validateIssue(myBorrower, myCopy)){             
                try{
                myLoan.saveToDatabase(); 
                }
                catch(Exception err){
                    String errString = "Could not save to db: " + err.toString();
                    AlertWindow aw = new AlertWindow(errString);
                    aw.setVisible(true);
                    System.out.println(errString);
                }
                
                AlertWindow aw = new AlertWindow("Loan has successfully been issued");
                aw.setVisible(true);
                
            }
          
        }
        catch(Exception err)
        {
            String errString = "Error: " + err.toString();
            AlertWindow aw = new AlertWindow(errString);
            aw.setVisible(true);
            System.out.println(errString);
        }
    }
    
    public boolean validateIssue(Borrower b, Copy c)
    {
          // TODO - Change this to a step by step process - 
          // First - is it reference only, - message to say cannot loan a ref only book
          //    Second - then is it on loan - message to say cannot loan a book on loan
          //        Third - then do you have 6 loans - message
          //            Fourth - do you have fines - message
          boolean myBool = false;
          String myMessage = "";
          if (c.isReferenceOnly() == false)
          {
              if(c.onLoan() == false){
                  if(b.getLoans().size()<6){
                      if(b.hasFines() == false){
                          myBool = true;
                      }
                      else{
                          // Fines Exist
                          myBool = false;
                          myMessage = myMessage + "</br> *There Are outstanding Fines to be paid";
                      }
                  }
                  else{
                      //Loans = 6 or greater
                      myBool = false;
                      myMessage = myMessage + "</br> *The Borrower Already Has 6 Loans";
                  }
              }
              else{
                  myBool = false;
                  myMessage = myMessage + "</br> *The Item is alread on Loan";
              }
          }
          else{
              myBool = false;
              myMessage = myMessage + "</br> *The Item is reference Only";
              //Reference Only
          }
          
          if (myMessage !=""){
          
          AlertWindow aw = new AlertWindow(myMessage);
          aw.setVisible(true);

          System.out.println(myMessage);
          }
          
          return myBool;
    }
    
    public boolean canBorrow(Item i, Borrower b){
        
       boolean canBorrowBoolean = !i.isReserved();
        // If reserved, we check to see if we're the first person to reserve
        if (i.isReserved() == true){
            String id = b.getBorrowerID();
            if (i.getFirstReserver().getBorrowerID().equals(id)){
                canBorrowBoolean = true;
                // We then need to remove the reservation
                Reservation removedReservation = new Reservation();
                removedReservation.setLibraryMemberID(id);
                removedReservation.setItemIdentifier(i.itemIdentifier);
                
                AlertWindow aw = new AlertWindow("Your reservation has been removed");
                aw.setVisible(true);
            }
            else 
            {
                AlertWindow aw = new AlertWindow("A loan cannot be issued because the item is already reserved.");
                aw.setVisible(true);
            }
        }
        return canBorrowBoolean;
    }



        
 
    
}
